package com.smartstate.EMS.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.smartstate.common.util.excelPro.ExcelExportDto;
import com.smartstate.common.util.excelPro.ExcelImportDto;
import com.smartstate.common.util.excelPro.ExportUtil;
import com.smartstate.common.util.excelPro.ImportUtil;
import com.smartstate.common.web.exception.BaseErrorEnum;
import com.smartstate.common.web.exception.CommonException;
import com.smartstate.EMS.dto.PageDTO;
import com.smartstate.EMS.dto.ToolDTO;
import com.smartstate.EMS.entity.Tool;
import com.smartstate.EMS.mapper.ToolMapper;
import com.smartstate.EMS.service.ToolService;
import com.smartstate.EMS.util.querywapper.WrapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;

@Transactional
@Service
@Slf4j
public class ToolImpl extends ServiceImpl<ToolMapper, Tool> implements ToolService {

  @Override
  public Page queryAllPageByDTO(PageDTO pageDTO) {
    //创建查询语句包装器和分页查询表对象
    Page<Tool> page = new Page(pageDTO.getPage(),pageDTO.getSize());
    //根据DTO注解封装查询语句
    Object dto = pageDTO.getDto();
    QueryWrapper<Tool> wrapper = WrapperUtil.convertQuery(dto,Tool.class);
    //返回分页查询结果
    return baseMapper.selectPage(page, wrapper);
  }

  public List<Tool> queryAllByDTO(ToolDTO dto){
    QueryWrapper<Tool> wrapper = WrapperUtil.convertQuery(dto,Tool.class);
    return baseMapper.selectList(wrapper);
  }


  @Override
  public int updateByDTO(Object dto) {
    UpdateWrapper<Tool> wrapper = new UpdateWrapper();
    WrapperUtil.convertUpdate(wrapper,dto);
    return baseMapper.update(null,wrapper);
  }


  /**
   * 导入数据
   * @param file
   * @return
   */
  @Override
  public int importData(MultipartFile file) {
    //excel转成对象
    ExcelImportDto<Tool> excelImportDto = ImportUtil.excel2EntityList(file, Tool.class);
    if(!excelImportDto.isError()) {
      List<Tool> entityLists = excelImportDto.getEntityList();
      //修改对象数据
      saveBatch(entityLists);
      return entityLists.size();
    }else{
      throw new CommonException(JSON.toJSONString(excelImportDto.getErrorMessage()), BaseErrorEnum.SERVER_ERROR.getCode(),excelImportDto.getErrorMessage());
    }
  }

  /**
   * excel导出模板下载
   * @return 导出对象
   */
  @Override
  public ExcelExportDto exportTemplate() {
    List<Tool> dataList = Collections.emptyList();
    return ExportUtil.exportData(dataList, Tool.class, "刀具模板", "刀具模板.xls", null);
  }

  /**
   * 导出单表数据
   * @return
   */
  @Override
  public ExcelExportDto exportData(ToolDTO dto) {
    return ExportUtil.exportData(queryAllByDTO(dto), Tool.class, "刀具数据", "刀具数据.xls", null);
  }


}
